function getBody(data) {
    var a = 0;
    var result = "";

    for (a = 0; a < data.length - 5; a++) {
        if (data[a] == "b" && data[a + 1] == "o" && data[a + 2] == "d" && data[a + 3] == "y")
            break;
    }

    for (a++; a < data.length - 5; a++) {
        if (data[a] == ">") break;
    }
    for (a++; a < data.length - 5; a++) {
        if (data[a] == "<" && data[a + 1] == "/" && data[a + 2] == "b" && data[a + 3] == "o" && data[a + 4] == "d" && data[a + 5] == "y" && data[a + 6] == ">")
            break;
        result += data[a];
    }

    return result;
}

var retrievedData = new Array();
var totalLink = 0;
var retrievedLink = 0;
var page = 1;
var productLinks = new Array();
var fromPage = 1;
var toPage = 2;
var temp;

function extractProductInfo_business_en(tempData) {
    var object = {};

    object['source'] = 'http://www.intelasia.net';
    object['language'] = 'English';

    var title = $('#content .entry h1', tempData).text();
    var short_content = '';
    var content = $('#content .entry p', tempData).text();
    var thumbnail_path = '';
    var news_type = 'Business';
    object['title'] = $.trim(title);
    object['short_content'] = short_content.substring(10,short_content.length);
    object['content'] = $.trim(content);
    object['news_type'] = $.trim(news_type);
    object['thumbnail_path'] = $.trim(thumbnail_path);
    return object;
}

//Lấy html của trang chi tiết sản phẩm
function getProductInfoHtmlFromUrl_business_en(url_site,url) {
    $.ajax({
        type:"post",
        url:url_site + 'data-mining/get-data',
        data:{ "site":url },
        async:true,
        success:function (data) {
            retrievedLink++;
//            $('#retrievedLink').html(retrievedLink);

            var modifiedData = getBody(data);
            var info = extractProductInfo_business_en(modifiedData);
            retrievedData.push(info);

            if (retrievedLink == totalLink) {
                for (a = 0; a < retrievedData.length; a++) insertProduct_business_en(url_site,retrievedData[a]);
                retrievedData = new Array();
            }
        },
        error:function () {
//            alert('error');
            retrievedLink++;
//            $('#retrievedLink').html(retrievedLink);
            var info = {};
            retrievedData.push(info);

            if (retrievedLink == totalLink) {
                for (a = 0; a < retrievedData.length; a++) insertProduct_business_en(url_site,retrievedData[a]);
                retrievedData = new Array();
            }
        }
    });
}

// Lấy tất cả sản phẩm từ trang hiển thị sản phẩm them loại sản phẩm.
function getProductsFromCategoryUrl_business_en(url_site,url) {
    $.ajax({
        type:"post",
        url:url_site + 'data-mining/get-data',
        data:{ "site":url },
        async:true,
        success:function (data) {
//            alert(data);
//            print('Retrieved product links complete : ' + url);

            var temp = getBody(data);
            //temp.html(getBody(data));
            //alert(getBody(data));
//            var temp = $('body',data).html();
//            $('body').html(data);

            // Get links
            temp = $('#content .post-title a', temp);
//            alert(temp.length);
            // Get product links
            if (temp.length > 0) {

                // Get product links.
                temp.each(function (index, item) {
                    var link = $(item).attr('href');
                    productLinks.push(link);
                });
            }

            if (toPage == page) {

                totalLink = productLinks.length;
//                $('#totalLink').html(totalLink);
                for (a = 0; a < totalLink; a++) {
                    getProductInfoHtmlFromUrl_business_en(url_site,productLinks[a]);
                }
                return;
            }
            // Get next page
            page++;
            temp = 'http://www.intelasia.net/category/vietnam/business' + '/' + page;
            getProductsFromCategoryUrl_business_en(url_site,temp);
        },
        error:function () {
//            alert("error category");
            print('Error retrieved product links : ' + url);

            if (toPage == page) {
                totalLink = productLinks.length;
//                $('#totalLink').html(totalLink);
                for (a = 0; a < totalLink; a++) {
                    getProductInfoHtmlFromUrl_business_en(url_site,productLinks[a]);
                }
                return;
            }

            // Get next page
            page++;
            temp = 'http://www.intelasia.net/category/vietnam/business' + page;
            getProductsFromCategoryUrl_business_en(url_site,temp);
        }
    });
}

//Đưa dữ liệu vào biến tạm, xuống hàng cuối dòng
function print(text) {
}

function insertProduct_business_en(url_site,info) {
    $.ajax({
        type:"post",
        url:url_site + 'data-mining/post-data',
        data:$.param(info),
        dataType:'json',
        async:true,
        success:function (data) {
//            alert(data['title_database']);
//            print(data.title);
            alert("inserted 1 business");

        },
        error:function(){
//            alert("insert failed");
        }
    });
}

// Hiển thị kết quả lên textarea
function displayProduct(info) {
    var prop;
    for (prop in info) print(prop + " : " + info[prop]);
}

var resultText = "";

function initGlobalVar() {
    resultText = "";
    totalLink = 1;
    retrievedLink = 0;
    retrievedData = new Array();
    productLinks = new Array();
}
